<template>
  <div class="subject-content">
    <el-form>
      <el-alert
        :title="`共${total}条记录`"
        type="info"
        show-icon
        :closable="false"
        style="margin:20px 0"
      />
      <el-table
        :data="subjectData"
        stripe
        style="width: 100%"
      >
        <el-table-column
          fixed
          type="index"
          :index="indexMethod"
          label="序号"
          width="50"
        />
        <el-table-column
          prop="subjectName"
          label="学科名称"
          width="100"
        />
        <el-table-column
          prop="username"
          label="创建者"
          width="100"
        />
        <el-table-column
          prop="addDate"
          label="创建日期"
          width="110"
        />
        <el-table-column
          prop="isFrontDisplay"
          label="前台是否显示"
          width="110"
        />
        <el-table-column
          prop="twoLevelDirectory"
          label="二级目录"
        />
        <el-table-column
          prop="tags"
          label="标签"
        />
        <el-table-column
          prop="totals"
          label="题目数量"
        />
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button type="text" @click="handleCatalogue">学科分类</el-button>
            <el-button type="text" @click="handleLabel">学科标签</el-button>
            <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
            <el-button type="text" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
  </div>
</template>

<script>
import { subjectListAPI, delsubjectAPI } from '@/api/subjectManager'
export default {
  name: 'Subject',
  data() {
    return {
      // 学科列表数据
      subjectData: [],
      total: '',
      query: {
        page: 1,
        pagesize: 10
      }
    }
  },
  created() {
    this.subjectList()
  },
  methods: {
    // 获取学科列表
    async subjectList() {
      const resp = await subjectListAPI(1, 15)
      // 拿到对象数组中的值
      for (const [index, item] of resp.items.entries()) {
        console.log(index, item)
        item.isFrontDisplay = +item.isFrontDisplay ? '是' : '否'
      }
      this.subjectData = resp.items
      this.total = resp.counts
    },
    indexMethod(index) {
      // 当前页数据的序号+1 +（当前页码-1）*每页条数
      return index + 1 + (this.query.page - 1) * this.query.pagesize
    },
    // 学科分类
    handleCatalogue() {
      this.$router.push('/subjectManager/catalogue')
    },
    // 学科标签
    handleLabel() {
      this.$router.push('/subjectManager/label')
    },
    handleEdit() {

    },
    // 删除
    async handleDel(row) {
      await delsubjectAPI(row.id)
      this.subjectList()
      this.$message.success('删除成功')
    }

  }

}
</script>

<style>

</style>
